﻿
Partial Class admin_users
    Inherits Page
    Private _context As TaxiEntities

    Protected Sub FormView1_ItemDeleted(sender As Object, e As FormViewDeletedEventArgs) Handles FormView1.ItemDeleted
        SqlDataSource1.DataBind()
        GridView1.DataBind()
    End Sub

    Protected Sub FormView1_ItemDeleting(sender As Object, e As FormViewDeleteEventArgs) Handles FormView1.ItemDeleting

        Dim gkey As Guid = CType(GridView1.SelectedValue, Guid)

        GetContext()

        Dim t = _context.transfers.Where(Function(x) x.client_id = gkey)

        For Each tr As transfers In t

            _context.transfers.Remove(tr)

        Next

        _context.SaveChanges()


        Dim tor = _context.torders.Where(Function(x) x.taxi_id = gkey)

        For Each tr As torders In tor

            _context.torders.Remove(tr)

        Next

        _context.SaveChanges()


    End Sub
    Protected Sub FormView1_ItemInserting(sender As Object, e As FormViewInsertEventArgs) Handles FormView1.ItemInserting
        For i As Integer = e.Values.Count - 1 To 0 Step -1

            Dim dbl As Date = Now
            Dim sdbl As String
            If IsDate(e.Values(i)) = True Then

                dbl = CType(e.Values(i), Date)

                sdbl = dbl.Month & "/" & dbl.Day & "/" & dbl.Year & " " & dbl.Hour & ":" & dbl.Minute

            End If

            SqlDataSource1.InsertCommand = Replace(SqlDataSource1.InsertCommand, CType(("@" & e.Values.Keys(i)), String), CType(IIf(IsNumeric(e.Values(i)) = True, e.Values(i), IIf(IsDate(e.Values(i)) = True, IIf(e.Values(i).ToString() <> "True" And e.Values(i).ToString() <> "False", String.Format("'{0}'", sdbl), Val(e.Values(i))), "'" & e.Values(i) & "'")), String))

        Next

        SqlDataSource1.InsertCommand = Replace(SqlDataSource1.InsertCommand, "True", "1")
        SqlDataSource1.InsertCommand = Replace(SqlDataSource1.InsertCommand, "False", "0")

    End Sub

    Protected Sub FormView1_ItemUpdated(sender As Object, e As FormViewUpdatedEventArgs) Handles FormView1.ItemUpdated
        GridView1.DataBind()
    End Sub

    Protected Sub FormView1_ItemUpdating(sender As Object, e As FormViewUpdateEventArgs) Handles FormView1.ItemUpdating

        For i As Integer = e.NewValues.Count - 1 To 0 Step -1

            Dim dbl As Date = Now
            Dim sdbl As String
            If IsDate(e.NewValues(i)) = True Then

                dbl = CType(e.NewValues(i), Date)

                sdbl = dbl.Month & "/" & dbl.Day & "/" & dbl.Year & " " & dbl.Hour & ":" & dbl.Minute

            End If

            SqlDataSource1.UpdateCommand = Replace(SqlDataSource1.UpdateCommand, CType(("@" & e.NewValues.Keys(i)), String), CType(IIf(IsNumeric(e.NewValues(i)) = True, e.NewValues(i), IIf(IsDate(e.NewValues(i)) = True, IIf(e.NewValues(i).ToString() <> "True" And e.NewValues(i).ToString() <> "False", String.Format("'{0}'", sdbl), Val(e.NewValues(i))), "'" & e.NewValues(i) & "'")), String))

        Next

        SqlDataSource1.UpdateCommand = Replace(SqlDataSource1.UpdateCommand, "True", "1")
        SqlDataSource1.UpdateCommand = Replace(SqlDataSource1.UpdateCommand, "False", "0")

        'Response.Write(SqlDataSource1.UpdateCommand)
        'Response.End()

    End Sub

    Protected Sub FormView1_PageIndexChanging(sender As Object, e As FormViewPageEventArgs) Handles FormView1.PageIndexChanging

    End Sub

    Protected Sub GridView1_DataBound(sender As Object, e As EventArgs) Handles GridView1.DataBound

    End Sub

    Protected Sub GridView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles GridView1.SelectedIndexChanged

    End Sub
    Function GetContext() As TaxiEntities

        If IsNothing(HttpContext.Current.Application("_context")) = True Then

            HttpContext.Current.Application("_context") = New TaxiEntities

        End If
        _context = CType(HttpContext.Current.Application("_context"), TaxiEntities)
        Return CType(HttpContext.Current.Application("_context"), TaxiEntities)

    End Function

    Protected Sub EntityDataSource1_Selecting(sender As Object, e As EntityDataSourceSelectingEventArgs) Handles EntityDataSource1.Selecting

    End Sub
End Class
